/*
 * PostData.java
 *
 * Created on April 14, 2006, 7:06 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package Comm.Net;

import Activity.ActivityChooserScreen;
import Comm.Util.Base64;
import Comm.Net.BaseData;
import Comm.Util.Exceptions.NoBarsException;
import Comm.Util.Exceptions.PreferencesException;
import Comm.Util.Exceptions.ServerDownException;
import Comm.Util.Preferences;

import Experience.ExpSample;
import Comm.Util.Logger;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author dmgreen
 */
public class PostData extends BaseData {
    
    private static final String LOG_MESSAGE = "log";
    private static final String EXP_MESSAGE = "exp";
    
    protected static String BOUNDARY = "-----frontier\r\n"; 
    protected static String ENDBOUNDARY = "-----frontier--\r\n"; 
    
    private String myStatus = "UNSENT";
    
    
    byte dataOut[]=null;
    //dataOut=image;
 
                   
    String POST_META = "--" + BOUNDARY  +
                        "Content-Disposition: form-data; name=\"submit\"\r\n" +
                        "\r\n" +
                        "submit\r\n" +

                        "--" + BOUNDARY  +
                        "Content-Disposition: form-data; name=\"auth\"\r\n" +
                        "\r\n" +
                        "a457c19eef\r\n"; 
    
    String POST_END = "--" + ENDBOUNDARY +
                        "\r\n";
          
    private static String TEXT = "Content-type: text/plain\r\n";
      
    /** 
     *  These are not used and have been subsumed by functions which dynamically construct these headings
     *
     * private static String OCTET = "Content-type: application/octet-stream\r\n" +
     *                              "Content-transfer-encoding: base64\r\n" +
     *                              "\r\n";
     * 
     * private static String IMAGE = "Content-Disposition: form-data; name=\"img_file_%s\"; filename=\"a_filename.jpg\"\r\n" +
     *                              "Content-type: image/jpeg\r\n" +
     *                              "Content-transfer-encoding: base64\r\n";
     * 
     * private static String AUDIO = "Content-Disposition: form-data; name=\"an_audio_file\"; filename=\"a_filename.wav\"\r\n" +
     *                              "Content-type: audio/x-wav\r\n" +
     *                              "Content-transfer-encoding: base64\r\n" +
     *                              "\r\n";
     */ 
          
    protected int mySampleGlucose = 0; 
    protected byte[] mySampleImage = null;
    
    
    public PostData(String url, int uid) {
        super(url, uid);
        
        myPayload += POST_META;
        myPayload += getIDHeader();
    }
    
    public String getPayload() {
        return myPayload + POST_END;
    }
    
     public void addTags(String tags) {
        myPayload += "--" + BOUNDARY +
                "Content-Disposition: form-data; name=\"tags\"\r\n" +
                 "Content-type: text/plain\r\n" + 
                "\r\n" +
                 tags + "\r\n";
    }
    
    public void addText(String msg) {
        myPayload += "--" + BOUNDARY +
                 TEXT +
                "\r\n" + 
                 msg + "\r\n"; 
                 
    }
    
    
    protected String StringifyTags(Vector sample_tags) {
        
        String tag_list = "";
        Enumeration tags_en = sample_tags.elements();
        while(tags_en.hasMoreElements()) {
            
            if (!tag_list.equals("")) {
                tag_list += ";";
            }
            
            tag_list += tags_en.nextElement();
        }
        
        return tag_list;
    }
     
    protected void addGlucose(int glucose_reading) {
        // allow for ORIGINAL glucose readingn.
        mySampleGlucose = glucose_reading;
        myPayload += getGlucoseHeader(glucose_reading);
    }
    
    private String getGlucoseHeader(int pre_glucose) {
        String glucose_post =     "--" + BOUNDARY  +
                                  "Content-Disposition: form-data; name=\"glucose\"\r\n" +
                                  "\r\n" +
                                  pre_glucose + "\r\n";
        
        return glucose_post;
    }
    
    private String getIDHeader() {
        
        int user_id;
        
        try {
            user_id = Preferences.getPreferences().getUserID();       
        }
        catch (PreferencesException pe) {
            Logger.getLog().addLogMessage("In POST: error while fetching user ID, subsituting \"0\"", Logger.MSG_ERROR);
            user_id = 0;
        }
        
        
       String id_post =  "--" + BOUNDARY  +
                        "Content-Disposition: form-data; name=\"user_id\"\r\n" +
                        "\r\n" +
                        user_id+"\r\n";
       
       return id_post;
    }
    
    public int getSampleGlucose() {
        return mySampleGlucose;
    }
    
    public byte[] getSampleImage() {
        return mySampleImage;
    }
}
